export default {
    data() {
        return {
            TownList: [],
            SelectTown: {
                Index: 0
            },
            // 认证信息
            IdentityForm: {
                //身份：大户、网点
                RoleType: 1,
                //联系人
                Person: '',
                //联系电话
                Mobile: '',
                //认证类型
                IdentityType: 1,
                //身份证号码
                CardNo: '',
                //身份证姓名
                CardName: '',
                //身份证正面
                CardFrontImg: '',
                //身份证反面
                CardBackImg: '',
                //公司名称
                CompanyName: '',
                //营业执照
                CoLicenseImg: '',
                //营业执照号码
                CoTaxNo: '',
                Province: '',
                ProvinceId: '',
                City: '',
                CityId: '',
                County: '',
                CountyId: '',
                Town: '',
                TownId: '',
                Address: '',
                //种植面积
                ContractArea: '',
                //种植面积证明材料
                ContractAreaImg: '',
                GroupNo: '',
                RegistCode: ''
            },
            // 企业信息
            SupplierInfo: {
                CompanyName: '',
                CompanyImage: '',
                Person: '',
                Mobile: '',
                Province: '',
                ProvinceId: '',
                City: '',
                CityId: '',
                County: '',
                CountyId: '',
                Town: '',
                TownId: '',
                Address: '',
                Description: '',
                QrCodePath: '' //注册码
            },
            defaultImage: '/static/images/my/missing-face.png'
        }
    },
    onReady() {
        this.$refs.popupMpphone.open();
    },
    methods: {
        getTown(countyId) {
            this.$request('common', 'getAreaList', {
                areaid: countyId
            }).then(res => {
                if (res.code == 1) {
                    this.TownList = res.data;
                } else {
                    this.$dialog.msg('没有获取到您所在区域的乡镇信息');
                }
            });
        },
        townChange(e) {
            const index = e.detail.value;
            this.SelectTown.Index = index;
            //省市县取配供服务主体
            this.IdentityForm.Province = this.SupplierInfo.Province;
            this.IdentityForm.ProvinceId = this.SupplierInfo.ProvinceId;
            this.IdentityForm.City = this.SupplierInfo.City;
            this.IdentityForm.CityId = this.SupplierInfo.CityId;
            this.IdentityForm.County = this.SupplierInfo.County;
            this.IdentityForm.CountyId = this.SupplierInfo.CountyId;
            //乡镇客户自己选
            this.IdentityForm.Town = this.TownList[index].AreaName;
            this.IdentityForm.TownId = this.TownList[index].AreaCode;
        },
        // 角色切换
        roleTypeClick(roletype) {
            if (this.IdentityForm.RoleType == roletype) {
                return;
            }
            this.IdentityForm.RoleType = roletype;
        },
        // 通过微信授权获取手机号，并验证手机号是否已经注册
        checkPhone(userInfoData) {
            if (userInfoData.detail.errMsg.indexOf('no permission') >= 0) {
                this.$dialog.alert('授权失败', '小程序还未认证，无法通过微信授权登录，请联系您的管理员完成小程序认证', '我知道了').then(res => {
                    this.navBack();
                });
                return;
            }
            if (userInfoData.detail.errMsg.indexOf('user deny') >= 0) {
                this.$refs.popupMpcancel.open();
                return;
            }
            this.$dialog.loading('授权中...');
            var encryptedData = userInfoData.detail.encryptedData;
            var iv = userInfoData.detail.iv;
            this.$util.throttle(async () => {
                let params = {
                    sessionkey: this.sessionkey,
                    openid: this.openid,
                    encryptedData: userInfoData.detail.encryptedData,
                    iv: userInfoData.detail.iv
                };
                // 验证手机号是否已经注册
                let res = await this.$request('auth', 'checkPhone', params);
                this.$dialog.hide();
                if (res.code === 1) {
                    if (res.data.Status == 0) {
                        // 不可以入住
                        this.$dialog.alert('提示', '您已经提交实名认证，无需重复提交。', '我知道了').then(res => {
                            // 微信登录
                            this.getPhoneNumber(userInfoData);
                        });
                    } else {
                        // 可以入住
                        this.IdentityForm.Mobile = res.data.Mobile;
                        this.IdentityForm.RegistCode = res.data.RegistCode;
                    }
                } else {
                    this.$dialog.msg(res.msg || '微信授权失败');
                }
            });
        },
        loginSuccess(data) {
            this.$store.commit('setToken', {
                token: data.AccessToken,
                mark: data.LoginMark,
                expire: data.Expires
            });
            uni.switchTab({
                url: '/pages/index/index'
            });
        },
        // 获取服务企业信息
        async getSupplierInfo(groupNo) {
            return new Promise((resolve, reject) => {
                this.$request('share', 'supplier', {
                        GroupNo: groupNo
                    })
                    .then(res => {
                        if (res.code == 1) {
                            resolve(res.data);
                        } else {
                            this.$dialog.msg(res.msg);
                        }
                    })
                    .catch(err => {
                        this.$dialog.msg(err.msg);
                    });
            });
        },
        companyImgError() {
            this.SupplierInfo.CompanyImage = this.defaultImage;
        },
    }
}
